import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/home",
    name: 'home',
    component: () => import("@/views/Home.vue"),
  },
  {
    path: "/register",   // 注册
    name: 'register',
    component: () => import("@/views/user/register.vue"),
  },
  {
    path: "/login",   // 登陆
    name: 'login',
    component: () => import("@/views/user/login.vue"),
  },
  {
    path: "/infoList",
    name: 'infoList',
    component: () => import("@/views/info/infoList.vue"),
  },
  {
    path: "/message",            // 聊天主路由
    name: 'message',
    component: () => import("@/views/message/messageIndex.vue"),
    children: [
      {
        path: "",            // 聊天首页
        name: 'messagelist',
        component: () => import("@/views/message/MessageList.vue"),
      },
      {
        path: "userList",            // 好友
        name: 'userList',
        component: () => import("@/views/message/userList.vue"),
      },
      {
        path: "userInfo",            // 个人中心
        name: 'userInfo',
        component: () => import("@/views/message/userInfo.vue"),
      }
    ]
  },
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  let arr=['/login','/register']       // 不会被拦截的路由
  let isTrue=arr.includes(to.path)
  if (isTrue) {
    return next()
  } else {
    //获取在登陆时返回数据里面保存在内存中的token值
    const userdata = window.sessionStorage.getItem("userdata") || null
    if (userdata==null) {
      return next("/login")
    } else {
      next()
    }
  }
})

// 2.导出
export default router


